package com.eco
{
	import com.eco.*;
	import com.framework.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.*;
	
	
	//敌人的管理类，控制敌人的产生，与参数设定
	public class EnemyManager extends MovieClip
	{
		private var vecEnemy:Vector.<Enemy>;//储存敌人类实例的数组
		private var iEnemyNum:int;//敌人实例个数，为了简化计算而设
		private var enNewEnemy:Enemy;//敌人实例的引用，简化一些表达式
		private var ftFighter:Fighter;//卫士类的引用，用于获取卫士类的一些参数
		
		public var sprCreateEnemyCMD:Sprite;
		
		private var w:int;//记录舞台的宽和高
		private var h:int;
		
		public function EnemyManager( )
		{
			//敌人管理器的构造函数
			
			vecEnemy = new Vector.<Enemy>();
			iEnemyNum = 0;
			
			sprCreateEnemyCMD = new Sprite();
			//
			addEventListener( Event.ADDED_TO_STAGE, Initialize);
			sprCreateEnemyCMD.addEventListener( CreateObjectEvent.CREATE_ENEMY, CreateEnemy );
			
			ftFighter = null;
		}
		
		public function SetFighter(fighter:Fighter):void
		{
			ftFighter = fighter;
		}
		
		public function GetEnemyVector():Vector.<Enemy>
		{
			//返回敌人数组
			return vecEnemy;
		}
		
		/*public function CreateEnemy( mass:Number = 30, r:int = 5, type:int = 0):void
		{
			//创建敌人实例函数
			if( iEnemyNum>=1 )//测试用敌人数量上限
			{
				return;
			}
			enNewEnemy = new EnemyESaw();//根据 参数创建敌人实例
			enNewEnemy.SetFighter( ftFighter );
			enNewEnemy.x = Math.floor( Math.random()*(w-100) );
			enNewEnemy.y = Math.floor( Math.random()*(h-100) );
			vecEnemy.push(enNewEnemy);//将实例存入数组
			iEnemyNum = vecEnemy.length;//更新敌人个数变量
			addChild( enNewEnemy );
		}*/
		
		public function GetEnemyNum():int
		{
			//返回当前敌人个数
			return iEnemyNum;
		}
		
		private function Initialize(e:Event):void
		{
			//该函数将在ADDED_TO_STAGE事件发生后被触发
			//首先检测stage是否已被赋值，因为只有当本类的对象和父节点有关联时，stage信息才会被传递下来
			if( stage != null )
			{
				//设置EnemyField每X毫秒调用CreateEnemy函数生成新敌人
				//CreateEnemy中包含了限制敌人数量等判断条件
				//setInterval( CreateEnemy, 100 );
				
				//由于某些原因，舞台的长宽可能有变化，而原始长宽也是许多计算的依据
				w = stage.stageWidth;//因此要把初始时舞台的长宽记录下来，
				h = stage.stageHeight;
				
				//移除监听器
				removeEventListener( Event.ADDED_TO_STAGE, Initialize );
			}
		}
		
		public function Init():void
		{
			
		}
		
		private function CreateEnemy(e:CreateObjectEvent):void
		{ 
			switch (e.objectType)
			{
			case Enemy.ENEMY_AXE:
			{
				enNewEnemy = new EnemyAxe();
				break;
			}
			case Enemy.ENEMY_ESAW:
			{
				enNewEnemy = new EnemyESaw();
				break;
			}
			case Enemy.ENEMY_CRUDEOIL:
			{
				enNewEnemy = new EnemyCrudeOil();
				break;
			}
			case Enemy.ENEMY_DARKCRUDEOIL:
			{
				enNewEnemy = new EnemyDarkCrudeOil();
				break;
			}
			case Enemy.ENEMY_HOOK:
			{
				enNewEnemy = new EnemyHook();
				break;
			}
			case Enemy.ENEMY_SPEAR:
			{
				enNewEnemy = new EnemySpear();
				break;
			}
		}
		
		enNewEnemy.SetFighter( ftFighter );
		enNewEnemy.x = e.posX;
		enNewEnemy.y = e.posY;
		vecEnemy.push(enNewEnemy);//将实例存入数组
		iEnemyNum = vecEnemy.length;//更新敌人个数变量
		addChild( enNewEnemy );
		
		}
		
		public function Update(e:Event = null):void
		{
			var i:int = 0;
			for( i=0; i<iEnemyNum;)
			{
				vecEnemy[i].Update();
				if( vecEnemy[i].iState == Enemy.STATE_END )//此处应该是Enemy.STATE_END，测试时可以写STATE_DEAD
				{
					removeChild( vecEnemy[i] );
					vecEnemy.splice( i, 1 );
					iEnemyNum--;
				}
				else
				{
					i++;
				}
			}
		}
		
		public function Pause():void
		{
			var i:int = 0;
			for( i=0; i<iEnemyNum; i++)
			{
				vecEnemy[i].Pause();
			}
			
			for(i=0; i<sprCreateEnemyCMD.numChildren; i++)
			{
				(sprCreateEnemyCMD.getChildAt(i) as CreateEnemyCMD).Pause();
			}
		}
		
		public function Continue():void
		{
			var i:int = 0;
			for( i=0; i<iEnemyNum; i++)
			{
				vecEnemy[i].Continue();
			}
			
			for(i=0; i<sprCreateEnemyCMD.numChildren; i++)
			{
				(sprCreateEnemyCMD.getChildAt(i) as CreateEnemyCMD).Continue();
			}
		}
		
		public function Clear():void
		{
			sprCreateEnemyCMD.removeEventListener( CreateObjectEvent.CREATE_ENEMY, CreateEnemy );
			ftFighter = null;
			enNewEnemy = null;
		}
	}
}